"use strict";

module.exports = (sequelize, DataTypes) => {
  const Customer = sequelize.define(
    "Customer",
    {
      openid: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "归属用户的 openid（用于小程序端无需 token 查询）",
      },
      nickname: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "客户昵称",
      },
      tags: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "标签，多个用逗号分隔",
      },
      province: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "省",
      },
      city: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "市",
      },
      district: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "区",
      },
      address: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "详细地址",
      },
      mobile: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "手机号",
      },
      gender: {
        type: DataTypes.INTEGER,
        allowNull: true,
        comment: "性别 0=未知 1=男 2=女",
      },
      avatar: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "头像",
      },
      email: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: "邮箱",
      },
      gift_date: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: "赠礼时间",
      },
      owner_id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: "归属用户ID（小程序登录用户）",
      },
    },
    {
      tableName: "customers",
    }
  );
  if (process.env.SYNC_MODE === "true") {
    Customer.sync({ alter: true })
      .then(() => console.log("✅ 客户表同步成功"))
      .catch((err) => console.error("❌ 客户表同步失败:", err));
  }
  return Customer;
};
